package com.sinaapp.mjfinal.config.run;

import org.apache.log4j.Logger;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.tx.TxByActionKeyRegex;
import com.jfinal.plugin.activerecord.tx.TxByActionKeys;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.activerecord.tx.TxByMethods;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.sinaapp.mjfinal.config.mapping.PlatformMapping;
import com.sinaapp.mjfinal.config.routes.PlatformRoutes;
import com.sinaapp.mjfinal.constant.ConstantCache;
import com.sinaapp.mjfinal.constant.ConstantInit;
import com.sinaapp.mjfinal.plugin.SqlXmlPlugin;
import com.sinaapp.mjfinal.tools.ToolBeetl;
import com.sinaapp.mjfinal.tools.ToolCache;
import com.sinaapp.mjfinal.tools.ToolString;

/**
 * 项目初始化配置
 * @author binchao
 * 2016年2月28日
 */
public class MJfinalConfig extends JFinalConfig {

	private static Logger log = Logger.getLogger(MJfinalConfig.class);
	
	@Override
	public void configConstant(Constants me) {
		log.info("configConstant 缓存 properties");
    	PropKit.use("init.properties");

		log.info("configConstant 设置字符集");
		me.setEncoding(ToolString.encoding); 

		log.info("configConstant 设置是否开发模式");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		
		log.info("configConstant 视图Beetl设置");
		me.setMainRenderFactory(new BeetlRenderFactory());
		ToolBeetl.regiseter();
		
		log.info("configConstant 视图error page设置");
		me.setError404View("/common/404.html");
		me.setError500View("/common/500.html");


	}

	@Override
	public void configRoute(Routes me) {
		log.info("configRoute 手动注册路由");
		me.add(new PlatformRoutes());
	}

	@Override
	public void configPlugin(Plugins me) {
		log.info("注册paltform ActiveRecordPlugin");
		new PlatformMapping(me);
		
		log.info("缓存设置");
		if(ToolCache.getCacheType().equals(ConstantCache.cache_type_ehcache)){
			log.info("EhCachePlugin EhCache缓存");
			me.add(new EhCachePlugin());
			
		}else if(ToolCache.getCacheType().equals(ConstantCache.cache_type_redis)){
			log.info("RedisPlugin Redis缓存");
			String redisIp = PropKit.get(ConstantInit.config_redis_ip);
			Integer redisPort = PropKit.getInt(ConstantInit.config_redis_port);
			RedisPlugin systemRedis = new RedisPlugin(ConstantCache.cache_name_redis_system, redisIp, redisPort);
			me.add(systemRedis);
		}
		
		log.info("SqlXmlPlugin 解析并缓存 xml sql");
		me.add(new SqlXmlPlugin());
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		log.info("configInterceptor 配置开启事物规则");
		me.add(new TxByMethods("save", "update", "delete"));
		me.add(new TxByMethodRegex("(.*save.*|.*update.*|.*delete.*)")); // 2.1只支持单实例添加，多方法名匹配使用一个正则匹配
		
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void afterJFinalStart() {
		// 系统启动成功后，可以加载多线程（资源监控、、、、）、、、
		super.afterJFinalStart();
	}

	@Override
	public void beforeJFinalStop() {
		// 停止系统前，需要做些什么操作、、、
		
		super.beforeJFinalStop();
	}

	public static void main(String[] args) {
		JFinal.start("WebContent", 8888, "/", 5);
	}

}
